#!/bin/sh

# LOG_PATH="/tmp/tcpam_logs"
LOG_PATH="logs"

if [ ! -d "$LOG_PATH" ]; then
    mkdir -p $LOG_PATH
fi

log_file_num=$(ls $LOG_PATH -tr | grep tcp.*.log | wc -l)
# 日志文件数量大于5个，则删除最早的日志文件
if [ "$log_file_num" -gt 5 ]; then
    rm_log_file=$(ls $LOG_PATH -tr | grep tcp.*.log | head -n 1)
    rm $LOG_PATH/$rm_log_file
fi

while read input_line
do
    current_date=$(date +"%Y_%m_%d")
    echo "$input_line" >> $LOG_PATH/$1_$current_date.log
done
